﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Vehicle.Web.Pages.Role
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Main.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-08-27   Created by 杨光远 V1.00.0000
    /// 功能说明：角色管理页面
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Main:Vehicle.Web.Component.PageBase
    {
        #region---基本变量的实例化---
        BLL.Role b_Role = new BLL.Role();//实例化逻辑层角色变量
        BLL.Log b_Log = new Vehicle.BLL.Log(); //实例化逻辑层日志变量
        Model.ComponentsInfo m_ComponentsInfo = new Model.ComponentsInfo();//实例化通用实体层变量
        public string onshow;//定义没有数据时页面的显示内容
        #endregion

        #region ---基本操作---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lbtn_AllDelete.Attributes.Add("onClick", "return   confirm('删除该角色的同时会删除属于该角色的用户');");
                RepeaterBind();//绑定数据

            }
        }

        /// <summary>
        /// 分页控件和Repeater数据绑定
        /// </summary>
        public void RepeaterBind()
        {

            this.Anp_Page.RecordCount = b_Role.GetAllCount(new Model.ComponentsInfo("Role", "R_ID"));
            m_ComponentsInfo.startIndex = Anp_Page.StartRecordIndex;//该页的起始索引
            m_ComponentsInfo.endIndex = Anp_Page.EndRecordIndex;//该页结束索引
            b_Role.RepeaterBind(Rept, m_ComponentsInfo);//绑定DataList控件
            if (Rept.Items.Count == 0)//判断没有数据时页面的显示内容
            {
                onshow = "当前页面无数据";
            }
            else
            {
                onshow = "";
            }

        }
        /// <summary>
        /// 分页控件分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            RepeaterBind();
        }

        /// <summary>
        /// 全部删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtn_AllDelete_Click(object sender, EventArgs e)
        {
            int Count = 1;
            foreach (RepeaterItem item in Rept.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("Chk_Num");
                if (chk.Checked)
                {
                    Count++;
                    b_Role.Delete(new Model.ComponentsInfo("Admin", "R_ID", chk.ToolTip));//这边要把用户管理中相应的角色用户也一并删除
                    b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功删除角色（" + b_Role.GetModel(new Vehicle.Model.ComponentsInfo("Role", "R_ID", chk.ToolTip, "R_ID")).Rows[0]["R_Name"].ToString() + "）和相应的用户信息"));//添加日志
                    b_Role.Delete(new Model.ComponentsInfo("Role", "R_ID", chk.ToolTip));//删除角色
                } 

            }
            if (Count > 1)
            {
                ScriptManager.RegisterClientScriptBlock(UPanel, this.GetType(), "click", "alert('删除成功！')", true);
                RepeaterBind();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(UPanel, this.GetType(), "click", "alert('您还没有进行选择！')", true);
            }

        }

        /// <summary>
        /// Repeater绑定数据事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Rept_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                ((LinkButton)(e.Item.FindControl("Lbtn_Delete"))).Attributes.Add("onClick", "return   confirm('删除该角色的同时会删除属于该角色的用户');");
            }
        }
        /// <summary>
        /// ReptComm激发事件
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void Rept_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            { 
                b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功删除角色（" + b_Role.GetModel(new Vehicle.Model.ComponentsInfo("Role", "R_ID", e.CommandArgument.ToString(), "R_ID")).Rows[0]["R_Name"].ToString() + "）和相应的用户信息"));//添加日志
                b_Role.Delete(new Model.ComponentsInfo("Admin", "R_ID", e.CommandArgument.ToString()));//这边要把用户管理中相应的角色用户也一并删除
                b_Role.Delete(new Model.ComponentsInfo("Role", "R_ID", e.CommandArgument.ToString()));//删除角色
               
                ScriptManager.RegisterClientScriptBlock(UPanel, this.GetType(), "click", "alert('删除成功！')", true);
                RepeaterBind();
            }
        }

        #endregion
    }
}
       